//
//  ViewController.m
//  线程保活
//
//  Created by 崔博 on 2018/8/22.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "ViewController.h"
#import "BBThread.h"
@interface ViewController ()
@property (nonatomic, strong) BBThread * thread;
@property (nonatomic, assign) BOOL isStop;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _thread = [[BBThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [_thread start];
}
- (IBAction)stop1:(id)sender {
    [self performSelector:@selector(stop) onThread:self.thread withObject:nil waitUntilDone:NO];
}
// 这个方法线程保活
- (void)run{
    __weak typeof(self) weakSelf = self;
    NSLog(@"%@",[NSThread currentThread]);
    [[NSRunLoop currentRunLoop] addPort:[NSPort new] forMode:NSDefaultRunLoopMode];
    while (!weakSelf.isStop) {
           [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    NSLog(@"销毁了");
}
- (void)stop{
    self.isStop = YES;
    CFRunLoopStop(CFRunLoopGetCurrent());
    NSLog(@"%@",[NSRunLoop currentRunLoop]);
}
- (void)dealloc
{
//    NSLog(@"我销毁了");
    
    self.thread = nil;
    
}
@end
